package info.sean.main;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import info.sean.constant.Constants;
import info.sean.entity.Movie;
import info.sean.entity.StudentEntity;
import info.sean.entity.UserEntity;
import info.sean.enums.SexAndOther;
import info.sean.enums.TemplateIdSmsFlowEnum;
import info.sean.util.DateUtil;
import info.sean.util.Safes;
import info.sean.util.SplitListUtils;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.impl.ConfigurableMapper;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.impl.MapperFacadeImpl;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import sun.security.jca.Providers;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.security.Provider;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class TestAll {
     private static String DATE_PATTERN = "yyyy-MM-dd";



    /**
     * 测试日期的格式和工具类
     */
    @Test
    public void testDate(){


        Date date = DateUtils.addDays(new Date(), 0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
        String format = simpleDateFormat.format(date);

        try {
            Date parse = simpleDateFormat.parse(format);
            System.out.println("日期格式为："+format);

            int i = dateCompare(parse, new Date());
            System.out.println("比较的数值为："+i);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
    public static int dateCompare(Date date1, Date date2) {
        int i = 0;
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        String bigenDate = sm.format(date1);
        String endDate = sm.format(date2);
        sm.format(date2);

        try {
            i = sm.parse(bigenDate).compareTo(sm.parse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return i;
    }


    /**
     * 测试removeif
     */


     @Test
    public void  testRemoveIf(){

         ArrayList<String> gscBiddingNoList = new ArrayList<>();
         ArrayList<String> accBiddingNoList = new ArrayList<>();
         gscBiddingNoList.add("1");
         gscBiddingNoList.add("2");
         gscBiddingNoList.add("sean");
         accBiddingNoList.add("4");
         accBiddingNoList.add("7");
         accBiddingNoList.add("lan");
         System.out.println("arrayList"+gscBiddingNoList);
         System.out.println("objects"+accBiddingNoList);

         String s = accBiddingNoList.get(0);
         System.out.println(s);
         accBiddingNoList.removeIf(
                 accBiddingNoLists -> "4".equals(accBiddingNoLists)

         );



         accBiddingNoList.stream().forEach(gscBiddingNoLists-> System.out.println("---"+gscBiddingNoLists));
         if (CollectionUtils.isNotEmpty(accBiddingNoList)){

         System.out.println("gsc为："+accBiddingNoList);
         }

     }


     @Test
    public void testFutures(){

         List<Future>futures = new ArrayList<>();
         List<CompletableFuture> completableFutures = new ArrayList<>();


     }


     @Test
    public void testUUIDs(){


         System.out.println("开始获取uuid");
         String uuid = getUUID();
         System.out.println("获取的uuid是："+uuid);
         System.out.println("结束获取uuid");


         String prngAlgorithm = getPrngAlgorithm();
         System.out.println(prngAlgorithm);
     }
    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-", "");
    }
    private static String getPrngAlgorithm() {
        for (Provider p : Providers.getProviderList().providers()) {
            for (Provider.Service s : p.getServices()) {
                if (s.getType().equals("SecureRandom")) {
                    return s.getAlgorithm();
                }
            }
        }
        return null;
    }


    @Test
    public void testSafes(){

        ArrayList<String> gscBiddingNoList = new ArrayList<>();
        ArrayList<String> accBiddingNoList = new ArrayList<>();
        gscBiddingNoList.add("1");
        gscBiddingNoList.add("2");
        gscBiddingNoList.add("sean");
        accBiddingNoList.add("4");
        accBiddingNoList.add("7");
        accBiddingNoList.add("lan");
        System.out.println("arrayList"+gscBiddingNoList);
        System.out.println("objects"+accBiddingNoList);

        String last = Safes.last(gscBiddingNoList);
        System.out.println("gsc集合最后的元素为："+last);



    }


    @Test
    public void  testSplitListUtils(){

        ArrayList<String> gscBiddingNoList = new ArrayList<>();
        ArrayList<String> accBiddingNoList = new ArrayList<>();
        gscBiddingNoList.add("1");
        gscBiddingNoList.add("2");
        gscBiddingNoList.add("sean");
        accBiddingNoList.add("4");
        accBiddingNoList.add("7");
        accBiddingNoList.add("lan");
        gscBiddingNoList.addAll(accBiddingNoList);
        System.out.println("arrayList"+gscBiddingNoList);

        System.out.println("-----------------");

        List<String> strings = gscBiddingNoList.subList(0, 3);
        System.out.println("sub:"+strings);   //这里是返回这个fromindex -toindex 的元素 但是不包含toindex 元素


        System.out.println("objects"+accBiddingNoList);

        List<List<String>> lists = SplitListUtils.splitListData(1, gscBiddingNoList);  //意思是每个数据集合里有几个元素
//        eg: arrayList[1, 2, sean, 4, 7, lan]
//        objects[4, 7, lan]
//        封片数据为“：[[1, 2, sean], [4, 7, lan]]
        System.out.println("封片数据为“："+lists);



    }


    @Test
    public void  testEntity(){



        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(SexAndOther.name);
        userEntity.setAge(21);
        userEntity.setSex(SexAndOther.man);
        userEntity.setOccupation(SexAndOther.occupation);
        BigDecimal bigDecimal = new BigDecimal(SexAndOther.salary);
        userEntity.setSalary(bigDecimal);

        System.out.println("userEntity:"+userEntity);

//        StudentEntity studentEntity = new StudentEntity();
//        BeanUtils.copyProperties(userEntity,studentEntity);
//        System.out.println("studentEntity"+studentEntity);

/**
 *  对象字段不对应的就是会有classMapBuilder
 */
        DefaultMapperFactory build = new DefaultMapperFactory.Builder().build();
        ClassMapBuilder<UserEntity, StudentEntity> classMapBuilder = build.classMap(UserEntity.class, StudentEntity.class)
                .field("userName", "studentName")
                .field("sex","study");

        classMapBuilder.exclude("age")
                .exclude("salary")
                .exclude("sex");
        classMapBuilder.byDefault().register();

        MapperFacade mapperFacade = build.getMapperFacade();
        StudentEntity studentEntity = mapperFacade.map(userEntity, StudentEntity.class);

        System.out.println("studentEntity:" + studentEntity);


    }


    @Test
    public void  testListEntity(){
        DefaultMapperFactory build = new DefaultMapperFactory.Builder().build();
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(SexAndOther.name);
        userEntity.setAge(21);
        userEntity.setSex(SexAndOther.man);
        userEntity.setOccupation(SexAndOther.occupation);
        BigDecimal bigDecimal = new BigDecimal(SexAndOther.salary);
        userEntity.setSalary(bigDecimal);
        List<UserEntity> userEntityArrayList = new ArrayList<>();
        userEntityArrayList.add(userEntity);
        userEntityArrayList.add(userEntity);
        build.classMap(UserEntity.class,StudentEntity.class)
                .field("userName","studentName")
                .field("age","study")
                .field("age","age")
                .byDefault().register();
        List<StudentEntity> studentEntityList = build.getMapperFacade().mapAsList(userEntityArrayList, StudentEntity.class);
        System.out.println("studentEntityList:"+studentEntityList);
    }

    @Test
    public void testSystemLineSeparator(){

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("姓名"+SexAndOther.name).append(System.lineSeparator());
        stringBuilder.append(System.lineSeparator());
        stringBuilder.append("性别"+SexAndOther.man).append(System.lineSeparator());
        stringBuilder.append("职业"+SexAndOther.occupation).append(System.lineSeparator());
        String user = stringBuilder.toString();
        System.out.println("用户为：" + user);
        System.out.println("java原生分割器为：" + System.lineSeparator()+"u");
    }

    @Test
    public void testSqlAndSeparatingTokens(){
        StringTokenizer stringTokenizer = new StringTokenizer("select * from student as st","?",false);
        String nextToken = stringTokenizer.nextToken();
        System.out.println("返回的下一个标记为：" + nextToken);

    }

    @Test
    public void testStream(){
        ArrayList<String> gscBiddingNoList = new ArrayList<>();
        ArrayList<String> accBiddingNoList = new ArrayList<>();
        gscBiddingNoList.add("1");
        gscBiddingNoList.add("2");
        gscBiddingNoList.add("sean");
        accBiddingNoList.add("4");
        accBiddingNoList.add("7");
        accBiddingNoList.add("lan");
        gscBiddingNoList.addAll(accBiddingNoList);
        System.out.println("arrayList"+gscBiddingNoList);

        List<String> collectList = gscBiddingNoList.stream().filter(gscBiddingNoLists -> !gscBiddingNoLists.equals("sean"))
                .collect(Collectors.toList());
        String s = collectList.stream().findFirst().orElse(null);
        System.out.println("s:"+s);
        System.out.println("collectList"+collectList);
    }

    @Test
    public void testStreamMap(){

            List<String> words = Arrays.asList("hello", "world", "I", "love", "you0");
            words.stream()
                    .map(String::length)
                    .forEach(System.out::println);
             words.stream().forEach(System.out::println);

        System.out.println("------------------");
        List<Integer> integers = Arrays.asList(1, 2);
        List<Integer> integers1 = Arrays.asList(3, 4, 5);
        Stream<List<Integer>> stream = Stream.of(integers,integers1);
        stream.flatMap(list -> list.stream())
                .forEach(System.out::println);
    }


    @Test
    public void testStreamReduce(){

        Stream<Integer> integerStream = Stream.of(1, 2, 4,3, 5, 6, 7);
//        Optional<Integer> reduce = integerStream.reduce((x, y) -> x + y);

        Integer sum = integerStream.reduce((x, y) -> x + y).get();
        System.out.println(sum);

    }


    @Test
    public void testSingleton(){

        List < Integer > arr_l = new ArrayList < Integer > ();


        arr_l.add(10);
        arr_l.add(20);
        arr_l.add(30);
        arr_l.add(40);
        arr_l.add(50);
//        arr_l.add(50);
//        arr_l.add(50);


        System.out.println("Array List: " + arr_l);

        // By using singleton() method is to
        // remove the elements 50 by using the
        // help of removeAll() method in ArrayList
        Set<Integer> singleton = Collections.singleton(40);
        System.out.println("singleton:"+singleton);
        arr_l.removeAll(Collections.singleton(40));

        // Display singleton list
        System.out.println("Collections.singleton(40): " + arr_l);


    }




    @Test
    public void  testMinListStream(){
        Movie s3 =new Movie("王五",22);
        Movie s4 =new Movie("赵六",23);
        Movie s1 =new Movie("张三",20);
        Movie s5 =new Movie("马奇",24);
        Movie s2 =new Movie("李四",21);
        Movie s8 =new Movie("李四",28);
        List<Movie> movieList = new ArrayList<>();
        movieList.add(s5);
        movieList.add(s3);
        movieList.add(s4);
        movieList.add(s1);
        movieList.add(s2);
        movieList.add(s8);
        System.out.println("movieList = " + movieList);
        Movie movie = movieList.stream().sorted((O1, O2)->{
            //O1-O2是得到正值取的是最小的
            //O2-O1是负值渠道是最大的
            return O1.getLikes() - O2.getLikes();
        }).findFirst().orElse(null);
        System.out.println("movie = " + movie);
    }

    @Test
    public void  TestPropertyDescriptor (){
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(SexAndOther.name);
        userEntity.setAge(21);
        userEntity.setSex(SexAndOther.man);
        userEntity.setOccupation(SexAndOther.occupation);
        BigDecimal bigDecimal = new BigDecimal(SexAndOther.salary);
        userEntity.setSalary(bigDecimal);
        PropertyDescriptor propertyDescriptor = null;
        try {
            propertyDescriptor = new PropertyDescriptor("userName", userEntity.getClass());
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        assert propertyDescriptor != null;
        Method readMethod = propertyDescriptor.getReadMethod();  // 获取方法，可以反射获取值
        Object invoke = null;
        try {
            invoke = readMethod.invoke(userEntity);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println("invoke = " + invoke);
        System.out.println("invoke.toString() = " + invoke.toString());
    }



     private static  final String overdueFlowNoticeDate = "-1"+ Constants.SPLIT_1+"-3"+Constants.SPLIT_1+"-31"+Constants.SPLIT_1+"-61";
     private static  final String overdueFlowNoticeDate1 = "1"+ Constants.SPLIT_1+"2"+Constants.SPLIT_1+"3"+Constants.SPLIT_1+"0";
    @Test
    public void  TestSmsSend(){
        System.out.println("overdueFlowNoticeDate = " + overdueFlowNoticeDate);
        System.out.println("overdueFlowNoticeDate = " + overdueFlowNoticeDate1);
        String[] split = overdueFlowNoticeDate.split(Constants.SPLIT_1);
        String[] split1 = overdueFlowNoticeDate1.split(Constants.SPLIT_1);
        int length = split.length;
        int length1 = split1.length;
        System.out.println("length = " + length);
//        Date date = new Date();
//        Date todayMorning = DateUtil.getDate(date);
        String dateStr = "2022-09-15";
        Date todayMorning = info.sean.util.DateUtils.parseDateTime(dateStr);
        System.out.println("todayMorning = " + todayMorning);
        for (int i = 0; i < length; i++) {
            System.out.println("分割的负数为：" + Integer.valueOf(split[i]));
            assert todayMorning != null;
            System.out.println( "当前时间的前"+ (i + 1)+"天" + DateUtil.getDateString(DateUtils.addDays(todayMorning, Integer.parseInt(split[i]))));
            Date addDays = DateUtils.addDays(todayMorning, Integer.parseInt(split[i]));
            System.out.println( "当前时间的前"+ (i+1)+ "天" + DateUtil.getDateString(addDays));
            System.out.println("TemplateIdSmsFlowEnum.getEnum(split[i]) = " + TemplateIdSmsFlowEnum.getEnum(split[i]));
        }
        System.out.println("\"------------------------------------------------ \" = " + "------------------------------------------------ ");
        for (int i = 0; i < length1; i++) {
            System.out.println("分割的正数为：" + Integer.valueOf(split1[i]));
            assert todayMorning != null;
            System.out.println("逾期的第"+Integer.valueOf(split1[i])+"天" + DateUtil.getDateString(DateUtils.addDays(todayMorning, Integer.parseInt(split1[i]))));
            Date addDays1 = DateUtils.addDays(todayMorning, Integer.parseInt(split1[i]));
            System.out.println("逾期的第"+Integer.valueOf(split1[i])+"天" + DateUtil.getDateString(addDays1));
        }


    }

    @Test
    public void testqq() {

        ArrayList<String> gscBiddingNoList = new ArrayList<>();
        ArrayList<String> accBiddingNoList = new ArrayList<>();
        gscBiddingNoList.add("1");
        gscBiddingNoList.add("2");
        gscBiddingNoList.add("sean");
        accBiddingNoList.add("4");
        accBiddingNoList.add("7");
        accBiddingNoList.add("lan");

        List updateData = new ArrayList<>(gscBiddingNoList.size());
        for (int i = 0; i < gscBiddingNoList.size(); i++) {
            String debitBean = gscBiddingNoList.get(i);
            String biddingBean = accBiddingNoList.get(i);

            System.out.println("debitBean = " + debitBean);
            System.out.println("biddingBean = " + biddingBean);
        }


    }

    @Test
   public void  testStringBuilder(){
        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append("C5022");
//        stringBuilder.append(Constants.SPLIT);
//        stringBuilder.append("1000000");
//        stringBuilder.append(Constants.SPLIT);
//        stringBuilder.append("5551");
//        stringBuilder.append(Constants.SPLIT);
//        stringBuilder.append("国美易卡");
//        stringBuilder.append(Constants.SPLIT);
//        stringBuilder.append("3335");
//        System.out.println("stringBuilder.toString() = " + stringBuilder.toString());

        System.out.println("\"-----------------\" = " + "-----------------");
        stringBuilder.append("C5022").append(Constants.SPLIT).append("1000000").append(Constants.SPLIT).append("5551").append(Constants.SPLIT).append("国美易卡").append(Constants.SPLIT).append("3335");
        System.out.println("stringBuilder.toString() = " + stringBuilder.toString());
    }



    @Test
    public void  testStringConcat(){
        String authority = "lan:";
        String chars = "lanxiaopeng";
        String concat = authority.concat(chars);
        System.out.println("concat = " + concat);

        Date date = DateUtils.setDays(new Date(), 1);
        System.out.println("date = " + date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_PATTERN);
        String format = simpleDateFormat.format(date);


        try {
            Date parse = simpleDateFormat.parse(format);
            Date add = info.sean.util.DateUtils.add(new Date(), Calendar.DATE, -1);
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(DATE_PATTERN);
            String format1 = simpleDateFormat1.format(add);
            System.out.println("format1 = " + format1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("日期格式为："+format);




    }



    public static void main(String[] args) {

        System.out.println("hotfix代码");
    }


    @Test
    public void  testS(){
        System.out.println("功能代码代码");

        //① 使用consumer接口实现方法
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        Stream<String> stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
        stream.forEach(consumer);

        System.out.println("********************");

        //② 使用lambda表达式，forEach方法需要的就是一个Consumer接口
        stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
        Consumer<String> consumer1 = (s) -> System.out.println(s);//lambda表达式返回的就是一个Consumer接口
        stream.forEach(consumer1);
        //更直接的方式
        //stream.forEach((s) -> System.out.println(s));
        System.out.println("********************");

        //③ 使用方法引用，方法引用也是一个consumer
        stream = Stream.of("aaa", "bbb", "ddd", "ccc", "fff");
        Consumer consumer2 = System.out::println;
        stream.forEach(consumer);
        //更直接的方式
        //stream.forEach(System.out::println);


    }

    @Test
    public void  testStringFilter(){
        String capitalCodes = "H34234234,DFDF,41342134,FDSFA,F50001,F30002,G50001";
        String capitalCodess = "G50001";
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            String[] capitalCodesArray = capitalCodess.split(",");
            List<String> capitalCodesList = Arrays.asList(capitalCodesArray);
            List<String> flowSideList = capitalCodesList.stream().filter(capitalCode -> capitalCode.startsWith("F")).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(flowSideList)) continue;

            arrayList.add(i);
        }

        System.out.println("arrayList = " + arrayList);

        System.out.println("\"----------\" = " + "----------");
        Date mustPayDate;
        //逾期30天 按日处理
        String dateStr = "2022-10-12";
        Date todayMorning = info.sean.util.DateUtils.parseDateTime(dateStr);
        for (int i = 30; i < 91; i++) {
            mustPayDate = DateUtils.addDays(todayMorning, -i);
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(DATE_PATTERN);
            String format1 = simpleDateFormat1.format(mustPayDate);
            System.out.println(i +"天"+"mustPayDate = " + format1);
        }

    }

    @Test
    public void  testAssert(){
//         int i = -1;
//         assert i >0;
//        System.out.println("i = " + i);
        BigDecimal repayTotalAmount = new BigDecimal(550);
        Optional<?> optionalO = repayTotalAmount.compareTo(BigDecimal.ZERO) > 0 ? Optional.of(repayTotalAmount) : Optional.empty();
        System.out.println("optionalO.isPresent() = " + optionalO.isPresent());
        System.out.println("optionalO.get() = " + optionalO.get());
    }


}
